<?php

namespace Modules\Asset\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

class AssetRecord extends Model
{
    protected $guarded = [];
    protected $table = 'asset_records';
    static public $resourceName = '';

    public function scopeSearch($query, $key) 
    {
        return $query->whereHas('user', function ($q) use ($key) {
            return $q->where('phone', 'like', '%' . $key . '%')->orWhere('name', 'like', '%' . $key . '%');
        })->orWhere('description', 'like', '%' . $key . '%');
    }

    public function scopeAssetType($query, $type)
    {
        return $query->whereHas('asset', function ($q) use ($type) {
            return $q->where('type', $type);
        });
    }

    public function scopeType($query, $type)
    {
    	if ($type == 'in') {
    		$query->where('type', 1);
    	}
    	elseif ($type == 'out') {
    		$query->where('type', 0);
    	}

    	return $query;
    }

    static public function beforeGetList($optoins)
    {
    	if (@$optoins['type']) {
    		$optoins['scopes']['type'] = $optoins['type'];
    	}

    	return $optoins;
    }

    public function user() 
    {
        return $this->belongsTo('\Modules\User\Entities\User', 'user_id');
    }

    public function admin() 
    {
        return $this->belongsTo('\Modules\Admin\Entities\Admin', 'admin_id');
    }

    public function asset() 
    {
        return $this->belongsTo('\Modules\Asset\Entities\Asset', 'asset_id');
    }
}
